<?php
if( !defined( '_VALID_MOS' ) && !defined( '_JEXEC' ) ) die( 'Direct Access to '.basename(__FILE__).' is not allowed.' );

/**
*
* @package MediaLibrary
* @copyright 2009 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru);Rob de Cleen(rob@decleen.com)
* Homepage: http://www.joomlawebserver.com
* @version: 1.5.3 Basic $
*
**/



require_once ($mosConfig_absolute_path."/components/com_medialibrary/medialibrary.class.lend.php");
require_once ($mosConfig_absolute_path."/components/com_medialibrary/medialibrary.class.review.php");


/**
* Book database table class
*/
class mosMedialibrary_book extends mosDBTable {
	/** @var int Primary key */
	var $id=null;
	/** @var int */
	var $book_id=null;	
	/** @var int */
	var $isbn=null;
	/** @var datetime */	
	var $title=null;
	/** @var varchar(200) */
	var $imageURL=null;
	/** @var varchar(250) */
	var $URL=null;
	/** @var int */
	var $rating=null;
	/** @var varchar(200) */
	var $authors=null;
	/** @var varchar(200) */
	var $manufacturer=null;
	/** @var varchar(45)*/
	var $edition=null;
	/** @var varchar(300) */
	var $comment=null;
	/** @var varchar(45)  */	
	var $language=null;
	/** @var varchar(45) */	
	var $release_date = null;
	/** @var varchar(250) */	
	var $ebookURL = null;
	/** @var varchar(15) */
	var $price = null;
	
	/**
	* @param database - A database connector object
	*/
	function mosMedialibrary_book( &$db ) {
		$this->mosDBTable( '#__medialibrary_book', 'id', $db );
	}
	
	// overloaded check function
	function check() {
		global $medialibrary_configuration;

		if(trim( $this->title) == ''){
		  $this->_error = _MEDIALIBRARY_LABEL_B_TITLE;
		  return false;
		}
		
		// check for valid name
		if (trim( $this->isbn ) == '') {
			$this->_error = _MEDIALIBRARY_LABEL_ISBN;
			return false;
		}
		
		$this->_db->setQuery( "SELECT id FROM #__medialibrary_book "
			. "\nWHERE book_id='$this->book_id'");						
		$xid = intval( $this->_db->loadResult() );
		if ($xid && $xid != intval( $this->id )) {
			$this->_error = _MEDIALIBRARY_ADMIN_INFOTEXT_JS_EDIT_MEDIAID;
			return false;
		}

    // check for existing ISBN
		if($medialibrary_configuration['editmedia']['check']['isbn_asin'] == '1'){
			$this->_db->setQuery( "SELECT id FROM #__medialibrary_book "
				. "\nWHERE isbn='$this->isbn'");						
			$xid = intval( $this->_db->loadResult() );
			if ($xid && $xid != intval( $this->id )) {
				$this->_error = _MEDIALIBRARY_ADMIN_INFOTEXT_JS_EDIT_ISBN_ASIN;
				return false;
			}
		
		}
		return true;
	}
	
	/**
	 * Is used to load a book by the ISBN number
	 * @param string - the ISBN number
	 */
	function loadISBN($isbn){		
		$this->_db->setQuery( "SELECT * FROM $this->_tbl WHERE isbn='$isbn'" );
		return $this->_db->loadObject( $this );
	}
	
		

	function toXML(& $xmlDoc ){

		//create and append name element 
		$retVal = & $xmlDoc->createElement("book"); 
		
		$book_id = & $xmlDoc->createElement("book_id");
		$book_id->appendChild($xmlDoc->createTextNode($this->book_id));
		$retVal->appendChild( $book_id);
		
		$catid = & $xmlDoc->createElement("isbn");
		$catid->appendChild($xmlDoc->createTextNode($this->isbn));
		$retVal->appendChild( $catid);
		
		$title = & $xmlDoc->createElement("title");
		$title->appendChild($xmlDoc->createCDATASection($this->title));
		$retVal->appendChild( $title);
		
		$authors = & $xmlDoc->createElement("authors");
		$authors->appendChild($xmlDoc->createCDATASection($this->authors));
		$retVal->appendChild( $authors);
		
		$manufacturer = & $xmlDoc->createElement("manufacturer");
		$manufacturer->appendChild($xmlDoc->createCDATASection($this->manufacturer));
		$retVal->appendChild( $manufacturer);
				
		$releasedate = & $xmlDoc->createElement("releasedate");
		$releasedate->appendChild($xmlDoc->createTextNode($this->release_date));
		$retVal->appendChild( $releasedate);

		$language = & $xmlDoc->createElement("language");
		$language->appendChild($xmlDoc->createTextNode($this->language));
		$retVal->appendChild( $language);

		$rating = & $xmlDoc->createElement("rating");
		$rating->appendChild($xmlDoc->createTextNode($this->rating));
		$retVal->appendChild( $rating);

		$price = & $xmlDoc->createElement("price");
		$price->appendChild($xmlDoc->createTextNode($this->price));
		$retVal->appendChild( $price);

		$url = & $xmlDoc->createElement("url");
		$url->appendChild($xmlDoc->createTextNode($this->URL));
		$retVal->appendChild( $url);

		$imageURL = & $xmlDoc->createElement("imageURL");
		$imageURL->appendChild($xmlDoc->createTextNode($this->imageURL));
		$retVal->appendChild( $imageURL);

		$edition = & $xmlDoc->createElement("edition");
		$edition->appendChild($xmlDoc->createTextNode($this->edition));
		$retVal->appendChild( $edition);

		$ebookURL = & $xmlDoc->createElement("ebookURL");
		$ebookURL->appendChild($xmlDoc->createTextNode($this->ebookURL));
		$retVal->appendChild( $ebookURL);

		$comment = & $xmlDoc->createElement("comment");
		$comment->appendChild($xmlDoc->createCDATASection($this->comment));
		$retVal->appendChild( $comment);
		
		return $retVal;
	}


}
?>
